package com.slipper.system.codeless.controller;

import com.slipper.core.gson.GsonUtil;
import com.slipper.system.affix.service.AffixItemService;
import com.slipper.system.codeless.bo.MobileBO;
import com.slipper.system.codeless.vo.VisualTemplateVO;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.VisualTemplateBO;
import com.slipper.system.codeless.service.VisualTemplateService;
import com.slipper.constants.Constants;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@RestController
@RequestMapping("/system/visualTemplate")
public class VisualTemplateController {
    @Resource
    private VisualTemplateService visualTemplateService;
    @Resource
    private AffixItemService affixItemService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody VisualTemplateBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(visualTemplateService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody VisualTemplateBO bo) {
        return ResultUtil.successResult(visualTemplateService.queryList(bo));
    }

    @PostMapping(value = "/queryCategoryPage")
    public String queryCategoryPage(@RequestBody VisualTemplateBO bo) {
        return ResultUtil.successResult(visualTemplateService.queryCategoryPage(bo));
    }

    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody VisualTemplateBO bo) {
        return ResultUtil.successResult(visualTemplateService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody VisualTemplateBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(visualTemplateService.save(bo), bo);
    }

    @PostMapping(value = "/saveSimple")
    public String saveSimple(@RequestBody VisualTemplateBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(visualTemplateService.saveSimple(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody VisualTemplateBO bo) {
        return ResultUtil.deleteResult(visualTemplateService.delete(bo));
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody VisualTemplateBO bo) {
        VisualTemplateVO vo = new VisualTemplateVO();
        vo.setVisualTemplateList(visualTemplateService.queryList(bo));
        vo.getVisualTemplateList().forEach(item -> vo.getFileList().addAll(affixItemService.queryWithFile(Visual.parseFileId(item.getJsonText()))));
        String fileName = "dashboard-template-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(vo)));
    }

    @PostMapping(value = "/importData")
    public String importData(MobileBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }
        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            VisualTemplateVO vo = GsonUtil.fromJson(AESUtil.decrypt(content), VisualTemplateVO.class);
            visualTemplateService.saveList(vo.getVisualTemplateList());
            affixItemService.saveList(vo.getFileList());
            return ResultUtil.successResult("导入应用成功。");
        } catch (Exception e) {
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
